package notentool;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileFilter;


@SuppressWarnings("serial")
public class PopupSaveFile extends JFrame {
	JButton btnAbbrechen;
	JButton btnSpeichern;
	public JCheckBox chckbxSaveNoLogin;
	//public boolean saveNoLogin_ref; /* changes the saveNoLogin reference on checkbox action */
	public String lastOKSubmit; // to save the latest string after pressing OK
	
	private JPanel panelButtons;
	private JPanel panelData;
	private JTextField tfFileLocation;
	private JButton btnChooseFile;
	
	/* assign a checkbox as parameter to synchronize the selected property from the checkboxes */
	public PopupSaveFile(String title, JCheckBox saveNoLogin_ref) {
		constructor(title, saveNoLogin_ref);
	}
	
	public PopupSaveFile(String title){
		constructor(title, null);
	}
		
	/* necessary to overload the real constructor without redundant code */
	void constructor(String title, final JCheckBox saveNoLogin_ref){
		setForeground(Color.BLACK);
		setTitle("Datenstruktur speichern");
		setSize(new Dimension(343, 129));
		
		getContentPane().setLayout(new BorderLayout(0, 0));
		
		panelData = new JPanel();
		getContentPane().add(panelData, BorderLayout.CENTER);
		GridBagLayout gbl_panelData = new GridBagLayout();
		gbl_panelData.columnWidths = new int[] {165, 0, 0};
		gbl_panelData.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		gbl_panelData.rowWeights = new double[]{0.0};
		panelData.setLayout(gbl_panelData);
		
		tfFileLocation = new JTextField();
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.fill = GridBagConstraints.BOTH;
		gbc_textField.insets = new Insets(0, 0, 5, 5);
		gbc_textField.gridx = 0;
		gbc_textField.gridy = 0;
		panelData.add(tfFileLocation, gbc_textField);
		tfFileLocation.setColumns(10);
		
		btnChooseFile = new JButton("Ort");
		btnChooseFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser fc = new JFileChooser();
			    fc.setFileFilter( new FileFilter()
			    {
			      @Override public boolean accept( File f )
			      {
			        return f.isDirectory() ||
			          f.getName().toLowerCase().endsWith( ".xml" );
			      }
			      @Override public String getDescription()
			      {
			        return "XML-Datei";
			      }
			    } );
			   // TODO catch exception
			   int state = fc.showSaveDialog( rootPane );
			   if ( state == JFileChooser.APPROVE_OPTION )
			    {
			      File file = fc.getSelectedFile();
			      tfFileLocation.setText( file.getAbsolutePath() );
			    }
			    else
			      System.out.println( "Auswahl abgebrochen" );
			    //System.exit( 0 );
			}
		});
		btnChooseFile.setToolTipText("Wähle einen Ort per Dialog");
		GridBagConstraints gbc_btnChooseFile = new GridBagConstraints();
		gbc_btnChooseFile.insets = new Insets(0, 0, 5, 0);
		gbc_btnChooseFile.anchor = GridBagConstraints.EAST;
		gbc_btnChooseFile.fill = GridBagConstraints.VERTICAL;
		gbc_btnChooseFile.gridx = 1;
		gbc_btnChooseFile.gridy = 0;
		panelData.add(btnChooseFile, gbc_btnChooseFile);
		
		JPanel panelSaveLogin = new JPanel();
		getContentPane().add(panelSaveLogin, BorderLayout.SOUTH);
		panelSaveLogin.setLayout(new BorderLayout(0, 3));
		
		chckbxSaveNoLogin = new JCheckBox("Logindaten beim Speichern auslassen");
		chckbxSaveNoLogin.setToolTipText("Passwort und Matrikelnummer werden beim Speichern nicht mit in deiner Noten.xml Datei gespeichert.");
		chckbxSaveNoLogin.setHorizontalAlignment(SwingConstants.CENTER);
		chckbxSaveNoLogin.setSelected( (null == saveNoLogin_ref)?false:saveNoLogin_ref.isSelected() );
		
		
		panelSaveLogin.add(chckbxSaveNoLogin, BorderLayout.NORTH);
		
		 /* changes the saveNoLogin reference on checkbox item change 
		  * and set the checkbox property "selected"
		  * */
		chckbxSaveNoLogin.addItemListener( new ItemListener() {
			public void itemStateChanged(ItemEvent ie) {
				/* ask for chckbxSaveNoLogin doesn't work. i don't no why.*/
				chckbxSaveNoLogin.setSelected( (ItemEvent.SELECTED  == ie.getStateChange())? true : false );
				
				////System.out.println("checked " + chckbxSaveNoLogin.isSelected() + " " + ie.getStateChange() );
				
				/* return the checkbox status if the reference isn't null */
				if( null != saveNoLogin_ref )
					saveNoLogin_ref.setSelected( chckbxSaveNoLogin.isSelected() );
			}
		});
		
		panelButtons = new JPanel();
		panelSaveLogin.add(panelButtons, BorderLayout.SOUTH);
		panelButtons.setLayout(new GridLayout(1, 0, 0, 10));
		
		btnSpeichern = new JButton("OK");
		panelButtons.add(btnSpeichern);
		
		btnAbbrechen = new JButton("Abbrechen");
		panelButtons.add(btnAbbrechen);
	}
	
	/* fills the textfield tfFileLocation*/
	void setFileLocation(String loca){
		this.tfFileLocation.setText(loca);
	}
	
	/* returns the content from the textfield tfFileLocation*/
	String getFileLocation(){
		return this.tfFileLocation.getText();
	}
	
	void storeOKSubmit(){
		lastOKSubmit = tfFileLocation.getText();
	}
	
	void restoreOKSubmit(){
		tfFileLocation.setText(lastOKSubmit);
	}
}
